<?php

namespace catchAdmin\apidoctor\controller;

use catchAdmin\apidoctor\service\MbqyService;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;

class Mbqy extends CatchController
{
    /**
     * 已签约服务包列表
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdSignedServicePack(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $list = (new MbqyService())->getSignedServicePack(request()->userInfo['id'], $req['patientId'],$req['status']??1);

        $contract_status_test = config('dictionary.contract_status_test');
        //把服务周期遍历到服务包中
        foreach ($list as $key => $val) {
            $list[$key]['service_info']['children'][0]['cycle'] = $val['cycle'];
            $list[$key]['service_info']['children'][0]['contract_id'] = $val['id'];
            $list[$key]['service_info']['children'][0]['status'] = $contract_status_test[$val['status']];
        }

        //拼装服务包
        $packs = [];
        foreach ($list as $key => $val) {
            $packs[] = $val['service_info']['children'][0];

        }

        //拼装最后服务包信息
        $ret = [];
        foreach ($packs as $key => $val) {
            $ret[$key]['id']    = $val['id'];
            $ret[$key]['name']  = $val['name'];
            $ret[$key]['cycle'] = $val['cycle'];
            $ret[$key]['status'] = $val['status'];
            $ret[$key]['contract_id'] = $val['contract_id'];
            $content = '';
            foreach ($val['children'] as $k1 => $v1) {
                $content .= $v1['name'].',';
            }
            $ret[$key]['content'] = rtrim($content, ',');//去掉结尾的，
        }
        return CatchResponse::success($ret);
    }

    /**
     * 已签约服务包详情
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdSignedServicePackDetails(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new MbqyService())->getSignedServicePackDetails($req['contract_id'], $req['id']);
        return CatchResponse::success($ret);
    }
}

